package com.xxxx.rules;

import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;
import java.util.*;

/**
 * @ClassName: MappingShardingAlgorithm
 * @Description: 映射分片算法
 * @Author: ZhouWei
 * @Date: 2023-4-16 - 15:28
 */
public class MappingShardingAlgorithm implements StandardShardingAlgorithm<String> {

    private Map<String, String> mapping = new HashMap<>();

    @Override
    public void setProps(Properties props) {
        String property = props.getProperty("sharding-tuple");
        String[] splits = property.split(",");
        for (String split : splits) {
            String[] map = split.split("\\$");
            mapping.put(map[0], map[1]);
        }
    }

    @Override
    public Collection<String> getTypeAliases() {
        return new ArrayList<>();
    }

    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<String> preciseShardingValue) {
        return mapping.get(preciseShardingValue.getValue());
    }

    @Override
    public Collection<String> doSharding(Collection<String> collection, RangeShardingValue<String> rangeShardingValue) {
        return null;
    }


    @Override
    public String getType() {
        return "MAPPING";
    }

    @Override
    public void init() {

    }
}
